
import { _decorator, Vec3, Mesh, utils, MeshRenderer, Material, tiledLayerAssembler } from 'cc';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import { BaseTransform } from './BaseTransform';
const { ccclass, property } = _decorator;

@ccclass('Terrain')
export class Terrain extends BaseTransform {

    public static len: number = 30;
    protected static width: number = 0;
    protected static radianScope: number = 0;
    protected static meshData;
    protected static _mesh: Mesh;
    protected static get mesh(): Mesh {
        if (!this._mesh) {
            this.createMesh();
        }
        return this._mesh;
    }
    protected static createMesh() {
        this.meshData = {
            positions: [],
            normals: [],
            indices: [],
            uvs: [],
        };

        let r = GameConfig.GameRule.roadHalfWidth - 0.1;//半径
        this.radianScope = (GameConfig.GameRule.roadMaxRadian - GameConfig.GameRule.roadMinRadian) * 0.5;
        this.width = r * this.radianScope;
        let startRadian = Math.PI * 0.5 - this.radianScope * 0.5;
        let endRadian = startRadian + this.radianScope;
        let radianCount = 11;//圆周上的顶点数量
        let stepRadian = (endRadian - startRadian) / (radianCount - 1);
        let inPI = 1 / (endRadian - startRadian);
        let startUVx = startRadian * inPI;
        let len = this.len;
        let lenCount = 11;//Z轴线上的顶点数量
        let stepZ = len / (lenCount - 1);
        let inLen = 1 / len;
        //内侧表面
        for (let i1 = startRadian; i1 <= endRadian; i1 += stepRadian) {
            let s1 = Math.sin(i1);
            let c1 = Math.cos(i1);
            let x = r * c1;
            let y = -r * s1;
            let uvx = 1 - i1 * inPI + startUVx;
            for (let z1 = 0; z1 <= len; z1 += stepZ) {
                this.meshData.positions.push(x, y, -z1);
                this.meshData.normals.push(-c1, s1, 0);
                this.meshData.uvs.push(uvx, 1 - z1 * inLen);
            }
        }
        for (let rc = 0; rc < radianCount - 1; ++rc) {
            for (let z2 = 0; z2 < lenCount - 1; ++z2) {
                let i2 = rc * lenCount + z2;
                this.meshData.indices.push(i2, i2 + 1, i2 + lenCount, i2 + 1, i2 + lenCount + 1, i2 + lenCount);
            }
        }

        this._mesh = new Mesh();
        this._mesh = utils.createMesh(this.meshData);
    }

    protected initSub() {
        this.initCollScope();
        this.initMesh();
    }
    protected resetSub() {
        this.resetCollScope();
    }

    protected setData(data: { z: number }) {
        this.node.setPosition(0, 0, data.z);
        this.minRadian = -Terrain.radianScope * 0.5;
        this.maxRadian = Terrain.radianScope * 0.5;
        this.maxZ = data.z;
        this.minZ = this.maxZ - Terrain.len;
    }

    public checkColl(role: BaseTransform) {
        if (role.minRadian <= this.maxRadian && role.maxRadian >= this.minRadian) {
            return true;
        }
        return false;
    }

    public setType(type: number) {
        this.meshRender.setMaterial(this.mats[type - 1], 0);
        this.badCarType = type;
    }

    /**地形不利的车辆类型 */
    public get type() { return this.badCarType; }

    protected badCarType: number = 0;

    @property(MeshRenderer)
    protected meshRender: MeshRenderer = null;
    @property([Material])
    protected mats: Material[] = [];
    protected initMesh() {
        this.meshRender.mesh = Terrain.mesh;
    }
}
